﻿using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI;

namespace WebSite.ViewModels.Fields
{
    public class AutoCompleteField : FieldBase
    {
        private readonly string m_ActionName;
        private readonly string m_ControllerName;
        private readonly object m_RouteValues;

        public AutoCompleteField(string caption, string name, string value, string actionName, string controllerName, object routeValues = null)
            : base(caption, name, value)
        {
            m_RouteValues = routeValues;
            m_ActionName = actionName;
            m_ControllerName = controllerName;
        }

        public override IHtmlString Render(HtmlHelper htmlHelper)
        {
            return MvcHtmlString.Create(htmlHelper.Telerik().AutoComplete()
                                            .Name(Name)
                                            .Encode(false)
                                            .HtmlAttributes(new { style = "width:100%"})
                                            .DataBinding(binding => binding.Ajax().Select(m_ActionName, m_ControllerName, m_RouteValues))
                                            .Filterable(filtering => filtering.FilterMode(AutoCompleteFilterMode.Contains))
                                            .ToHtmlString());
        }
    }
}